﻿using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using Xoohoo.Extensions;
using Xoohoo.Models.InputModels;
using Xoohoo.Repositories.SqlServerEF.Entities;
using XM = Xoohoo.Models;

namespace Xoohoo.Repositories.SqlServerEF
{
    public class BulletinRepository : IBulletinRepository
    {
        private readonly XoohooContext _db = new XoohooContext();

        protected DbSet<Bulletin> DbSet
        {
            get { return _db.Bulletins; }
        }

        public XM.Bulletin GetItem()
        {
            return DbSet.FirstOrDefault().ToModel<XM.Bulletin>();
        }

        public bool Save(BulletinInput bulletin)
        {
            Bulletin dbBulletin = DbSet.FirstOrDefault();
            if (dbBulletin == null) return false;

            dbBulletin.UpdateFrom(bulletin);
            _db.SaveChanges();

            return true;
        }

        #region IDisposable Members

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _db.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
